// 获取地理信息并缓存
const fetchData = async (url: string) => {
  const response = await fetch(url);
  const data = await response.json();

  const cache = new Map<string, string>();
  data.features.forEach((feature: any) => {
    const { adcode, name } = feature.properties;
    cache.set(name, adcode);
  });
  const result = { data, cache: Array.from(cache.entries()) }
  return result;
}

// 监听主线程的消息
self.onmessage = async (e) => {
  const url = e.data;

  try {
    const result = await fetchData(url);
    // 返回结果给主线程
    self.postMessage(result);

  } catch (error: any) {
    self.postMessage({
      type: 'error',
      payload: `Worker 加载地理数据失败: ${error.message}`
    });
  }
};